//
//  FKPosterVC.m
//  WanTao
//
//  Created by Macbook Pro on 2019/4/19.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKPosterVC.h"
#import <Photos/PHAssetChangeRequest.h>
#import <UShareUI/UShareUI.h>

@interface FKPosterVC ()

@property (nonatomic, weak) UIView *container;
@property (nonatomic, weak) UIImageView *photoView;
@property (nonatomic, weak) UIImageView *qrcodeView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIImageView *avatarView;

@end

@implementation FKPosterVC

- (instancetype)init
{
    FKPosterVC *vc = [super init];
    // 防止presentViewController过后控制器的view的背景色变成黑色
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    return vc;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSDictionary *userInfo = [FKCacheTool userInfo];
    NSString *level = userInfo[@"level"];
    
    self.view.backgroundColor = [UIColor blackColor];
    
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(close)]];
    
    UIView *container = [[UIView alloc] init];
    [self.view addSubview:container];
    container.clipsToBounds = true;
    self.container = container;
    container.backgroundColor = [UIColor whiteColor];
//    [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
    
    UIView *blackView = [[UIView alloc] init];
    [self.view addSubview:blackView];
    blackView.backgroundColor = fkColorAlpha(0, 0, 0, 1);
    
    UIButton *saveBtn = [UIButton fk_btnWithTarget:self action:@selector(saveBtnClicked) font:fk_adjustFont(14) normalTitle:@"保存图片" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"save_icon"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [blackView addSubview:saveBtn];
    saveBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    saveBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 6, 0, 0);
    
    UIButton *shareBtn = [UIButton fk_btnWithTarget:self action:@selector(shareBtnClicked) font:fk_adjustFont(14) normalTitle:@"立即分享" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"share_icon"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [blackView addSubview:shareBtn];
    shareBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 6);
    
    UIImageView *photoView = [[UIImageView alloc] init];
    [container addSubview:photoView];
    self.photoView = photoView;
    photoView.contentMode = UIViewContentModeScaleAspectFill;
    
    UIImageView *iconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"new_logo"]];
    [container addSubview:iconView];
    
    UIView *bottomView = [[UIView alloc] init];
    [container addSubview:bottomView];
    
    UIImageView *qrcodeView = [[UIImageView alloc] init];
    [bottomView addSubview:qrcodeView];
    self.qrcodeView = qrcodeView;
    qrcodeView.backgroundColor = fkBgColor;
    
    
    
    [blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(60));
    }];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(100));
        make.left.offset(fk_adjustW(30));
        make.top.bottom.offset(0);
    }];
    [shareBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.top.bottom.equalTo(saveBtn);
        make.right.offset(fk_adjustW(-30));
    }];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.offset(10);
        make.right.offset(-10);
        make.bottom.equalTo(blackView.mas_top).offset(-10);
    }];
    [iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.top.offset(15);
        make.width.height.offset(fk_adjustW(40));
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(100));
    }];
    [photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.bottom.equalTo(bottomView.mas_top);
    }];
    [qrcodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(10));
        make.height.width.offset(fk_adjustW(80));
        make.centerY.offset(0);
    }];
    
    
//    if (![level isEqualToString:@"10"]) {
    if ([level isEqualToString:@"10"]) {
        // 直接盖一个imageView
        UIImageView *unvip = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"default_bottom_img"]];
        [bottomView addSubview:unvip];
        [unvip mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(fk_adjustW(177), fk_adjustW(80)));
            make.bottom.mas_equalTo(fk_adjustW(-6));
            make.right.mas_equalTo(fk_adjustW(-48));
        }];
    } else {
        UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
        [bottomView addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        UILabel *descLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor666666 text:@"我为你推荐"];
        [bottomView addSubview:descLabel];
        
        UIImageView *tipView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"easyBuy2"]];
        [bottomView addSubview:tipView];
        
        UIImageView *avatarView = [[UIImageView alloc] init];
        [bottomView addSubview:avatarView];
        self.avatarView = avatarView;
        [avatarView fk_viewCornerRadius:fk_adjustW(35) borderWidth:0 borderColor:nil];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(qrcodeView.mas_right).offset(40);
            make.top.equalTo(qrcodeView).offset(8);
        }];
        [descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(titleLabel);
            make.top.equalTo(titleLabel.mas_bottom).offset(4);
        }];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerX.equalTo(bottomView);
//            make.bottom.equalTo(bottomView.mas_bottom).offset(-20);
            make.width.offset(fk_adjustW(80));
            make.height.offset(fk_adjustW(35));
            make.left.mas_equalTo(titleLabel);
            make.top.mas_equalTo(descLabel.mas_bottom).offset(2);
        }];
        [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.offset(fk_adjustW(70));
            make.centerY.equalTo(qrcodeView);
            make.right.offset(-8);
        }];
        [avatarView sd_setImageWithURL:[NSURL URLWithString:userInfo[@"avatar"]] placeholderImage:nil];
    }
    
    [photoView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl] placeholderImage:nil];
    qrcodeView.image = [UIImage fk_imageGenerateQRCode:self.qrcodeUrl width:200 height:200];
    NSString *name = [NSString stringWithFormat:@"我是%@",userInfo[@"nickname"]];
    NSMutableAttributedString *nickname = [[NSMutableAttributedString alloc] initWithString:name];
    // red #DB2C5C
    [nickname setAttributes:@{NSFontAttributeName:fkFont12} range:NSMakeRange(0, 2)];
    [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#777777"]} range:NSMakeRange(0, 2)];
    [nickname setAttributes:@{NSFontAttributeName:fkFont16} range:NSMakeRange(2, nickname.length - 2)];
    [nickname setAttributes:@{NSForegroundColorAttributeName:[UIColor fk_colorWithHexString:@"#DB2C5C"]} range:NSMakeRange(2, nickname.length - 2)];
    self.titleLabel.attributedText = nickname;
}

- (void)saveBtnClicked
{
    [SVProgressHUD show];
    UIImage *image = [self.container fk_snapshotImage];
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (success) {
            [SVProgressHUD showSuccessWithStatus:@"保存成功"];
        } else {
            [SVProgressHUD showInfoWithStatus:@"保存失败"];
        }
    }];
}

- (void)shareBtnClicked
{
    [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
        UIImage *image = [self.container fk_snapshotImage];
        UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
        UMShareImageObject *shareObject= [[UMShareImageObject alloc] init];
        shareObject.shareImage = image;
        messageObject.shareObject = shareObject;
        [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:self completion:^(id data, NSError *error) {
            if (error) {
                [SVProgressHUD showErrorWithStatus:@"分享失败"];
            } else {
                [SVProgressHUD showSuccessWithStatus:@"分享成功"];
            }
        }];
    }];
}

- (void)close
{
    [self dismissViewControllerAnimated:true completion:nil];
}

@end
